package com.cc;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.cc.entity.User;
import com.cc.mapper.UserMapper;

public class TestCache {
    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = null;
        SqlSession sqlSession2 = null;
        try {
            sqlSession = sqlSessionFactory.openSession();
            sqlSession2 = sqlSessionFactory.openSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);

            User user1 = userMapper.selectUser(33);
            System.out.println("use1===>" + user1);

            // 曾删改
//            user1.setAddress("太行山");
//            userMapper.updateUserById(new User(27,"11",new Date(),"4","22"));

            sqlSession.clearCache();
            sqlSession.close();
            // 流程：数据从数据进行查询，放到一级缓存，当sqlSession关闭时，
            // 数据才会刷新到二级缓存
            User user2 = userMapper2.selectUser(33);
            System.out.println("user2===>" + user2);

        } finally {

            sqlSession2.close();
        }
    }
}
